﻿using Microsoft.Extensions.Logging;
using S.EarTag.Domain.Model;
using S.EarTag.Infrastructure.Repository.Interface.IEarTagRepository;
using SqlSugar;
using SqlSugarTests.WebAPI.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace S.EarTag.Infrastructure.Repository.Impl.EarTagRepository
{
    public class MarkModelRepository : BaseRepository<MarkModel>, IMarkModelRepository
    {
        private readonly SqlSugarClient _db;
        public MarkModelRepository(SqlSugarClient db) : base(db)
        {
            _db = db;
        }
        /// <summary>
        /// 戴标批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> BatchRemoveMaek(List<int> ids)
        {
            var result=await _db.Deleteable<MarkModel>().In(ids).ExecuteCommandAsync();
            return result > 0;
        }
       

        /// <summary>
        /// 多条件查询戴标数据
        /// </summary>
        /// <param name="EarTag"></param>
        /// <param name="NewEITag"></param>
        /// <param name="EITag"></param>
        /// <param name="WearId"></param>
        /// <param name="MarkState"></param>
        /// <returns></returns>
        public async Task<List<MarkModel>> GetMarkListPage(string? EarTag, string? NewEITag , string? EITag, int WearId, string? MarkState)
        {
            var query = await _db.Queryable<MarkModel>()//.Where(a => a.EarTag == EarTag).ToListAsync();
            .Where(a => (string.IsNullOrEmpty(EarTag) || a.EarTag.Contains(EarTag)) &&
            (string.IsNullOrEmpty(EITag) || a.EITag.Contains(EITag)) &&
            (string.IsNullOrEmpty(NewEITag) || a.NewEITag.Contains(NewEITag)) &&
             (WearId == 0 || a.WearId == (WearItem)WearId) &&
             (string.IsNullOrEmpty(MarkState)) || a.MarkState.Contains(MarkState)).ToListAsync();
            return query;
        }
        /// <summary>
        /// 查询电子耳标状态为未使用的数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<MarkModel>> GetMarkModels()
        {
            var list=await _db.Queryable<MarkModel>().Where(a=>a.UseState=="未使用").ToListAsync();
            return list;
        }
        /// <summary>
        /// 根据电子耳标主键Id修改使用状态
        /// </summary>
        /// <param name="MarkId"></param>
        /// <returns></returns>
        public async Task<bool> UpdDateMark(int MarkId)
        {
            string sql = "UPDATE MarkModel SET  UseState= '已使用' WHERE MarkId = " + MarkId;
            var result=await _db.Ado.ExecuteCommandAsync(sql);
            return result > 0;
        }
        /// <summary>
        /// 根据可视耳标修改状态为已使用
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        public async Task<bool> UpdMarkStateEarTag(string? EarTag)
        {
            string sql = "UPDATE MarkModel SET  UseState= '已使用' WHERE EarTag = '" + EarTag + "'";
            var result = await _db.Ado.ExecuteCommandAsync(sql);
            return result > 0;
        }
        /// <summary>
        /// 根据可视耳号修改状态为已使用
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        public async Task<bool> UpdMarkStateEarTagTwo(string? EarTag)
        {
            string sql = "UPDATE MarkModel SET  UseState= '未使用' WHERE EarTag = '" + EarTag + "'";
            var result = await _db.Ado.ExecuteCommandAsync(sql);
            return result > 0;
        }
        /// <summary>
        /// 根据Id批量查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<List<MarkModel>> GetMarkids(string ids)
        {
            var result = await _db.Ado.SqlQueryAsync<MarkModel>($"SELECT * FROM [dbo].[MarkModel] WHERE MarkId in({ids})");
            return result.ToList();
        }
        /// <summary>
        /// 查询重复值
        /// </summary>
        /// <param name="EITag"></param>
        /// <returns></returns>
        public async Task<List<MarkModel>> GetMarkEITag(string? EITag)
        {
            var result=await _db.Queryable<MarkModel>().Where(a=>a.EITag == EITag).ToListAsync();
            return result.ToList();
        }
    }
}
